package calcul;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.apache.commons.collections.map.MultiKeyMap;

import enums.CableType;
import generatepath.CableTypeLengthValuesPair;

public class CalculatePDV {


	public ArrayList<Dictionar> calculate(ArrayList<String> paths,
			MultiKeyMap multiKeyMapCableTypeLength) {
		ArrayList<Dictionar> rasp = new ArrayList();
		for (String path : paths) {
//			System.out.println(path);
			int counter = 0, tokenLength;
			StringTokenizer str = new StringTokenizer(path, ";");
			tokenLength = str.countTokens();
			String initial = str.nextToken();
			double totalPVD = 0;
			while (str.hasMoreTokens()) {
				double lengt;
				double valueBaza;
				double retinerea;
				double totalTemp;
				String curent = str.nextToken();
				CableTypeLengthValuesPair temp = (CableTypeLengthValuesPair) multiKeyMapCableTypeLength
						.get(initial, curent);
				if (counter == 0) {
					lengt = temp.getLength();
//					System.out.println("left");
					valueBaza = temp.getValue().getLeft();
					retinerea = temp.getValue().getRetinerea();
					totalTemp = lengt * retinerea + valueBaza;
					totalPVD += totalTemp;
				} else if (counter == tokenLength - 2) {
//					System.out.println("right");
					lengt = temp.getLength();
					valueBaza = temp.getValue().getRight();
					retinerea = temp.getValue().getRetinerea();
					totalTemp = lengt * retinerea + valueBaza;
					totalPVD += totalTemp;
				} else {
//					System.out.println("midle");
					lengt = temp.getLength();
					valueBaza = temp.getValue().getMiddle();
					retinerea = temp.getValue().getRetinerea();
					totalTemp = lengt * retinerea + valueBaza;
					totalPVD += totalTemp;
				}
//				System.out.println("length "+ lengt+" valueBaza "+valueBaza+" retinerea "+retinerea+" totalPVD "+totalPVD+" cableType "+temp.getValue().name());
				counter++;
				initial = curent;
			}
//			System.out.println("TOTAL PVD " + totalPVD + "\n");
			rasp.add(new Dictionar(path, totalPVD));
		}
		return rasp;
	}
}
